app.controller("switchBranchCtrl", ["$scope", "$rootScope", "$modalInstance", "$http", "info", function ($scope, $rootScope, $modalInstance, $http, info) {
    $scope.info = info;
    $scope.info.radioStatus = "true";
    $scope.info.planInfo = {};
    //选择已有项目
    $scope.info.proList = [];
    $scope.info.selectProName = '';
    //选择新建项目
    $scope.info.svn_form = '';
    $scope.info.proInfo = {};
    $scope.info.msg = '';

    $scope.getPlanInfo = function () {
        var url = beetle_plan_url + '/project_plan/getPlanList?hasProList=true&isShowOnline=true&token_id=' + getTokenId() + '&plan_id=' + $scope.info.planId;
        $http.get(url).success(function (data) {
            $scope.info.planInfo = data.data[0];
        })
    };

    $scope.getPlanInfo();

    //获取主干下工程列表
    $scope.getProListByTrunk = function () {
        var postParams = {
            tokenId: getTokenId(),
            trunkName: $scope.info.trunkName
        };
        var url = java_url + "/beetle/project/projectListByTrunk";
        $.ajax({
            type: 'post',
            url: url,
            data: postParams,
            dataType:'json',
            success: function (data) {
                if (data.code == '0') {
                    $scope.$apply(function () {
                        $scope.info.proList = data.data.list;
                        if ($scope.info.proList.length > 0) {
                            $scope.info.selectProName = $scope.info.proList[0].projectName;
                        }
                    })
                }
            },
            error: function (data) {
                alert(data.desc)
            }
        })
    };

    $scope.updateBranch_num = function () {
        var branch_num = $scope.info.proInfo.branch_num;
        var t = /^(\d{1,4}){1}(\.\d{1,4}){2}$/;
        if (branch_num.match(t)) {
            $scope.info.msg = '';
        } else {
            $scope.info.msg = '请输入正确的版本号！格式：0.0.0';
        }
    };

    $scope.updatePlanCode = function () {
        if ($scope.info.planInfo.plan_code != '') {
            $scope.info.msg = '';
        } else {
            $scope.info.msg = '项目代号不能为空!';
        }
    };

    $scope.radioChange = function () {
        if ($scope.info.radioStatus == "true") {
            var list = [$scope.info.trunkName];
            var url = beetle_plan_url + "/project_plan/getPlanProjectDefaultName?token_id=" + getTokenId() + "&plan_code=" + $scope.info.planInfo.plan_code + "&trunk_list=" + JSON.stringify(list);
            $http.get(url).success(function (data) {
                $scope.info.proInfo = data.data[0];
                $scope.info.svn_form = $scope.info.proInfo.svn_list[0].name;
            })
        } else {
            $scope.getProListByTrunk();
        }
    };
    $scope.radioChange();
    //切换
    $scope.changeToBranch = function () {
        var confirmParams = {
            "titleData": "提示",
            "bodyData": "确定要切换到新工程吗？"
        };
        $rootScope.confirmWindow(confirmParams, function (data) {
            if (!data) {
                return false;
            }
            var confirm = {
                "titleData": "提示",
                "bodyData": "切换后，将使用新工程进行开发与提测，请再次确认！"
            };
            $rootScope.confirmWindow(confirm, function (d) {
                if (!d) {
                    return false;
                }
                if ($scope.info.radioStatus == "true") {
                    var projectName = $scope.info.proInfo.trunk_name + '-' + $scope.info.proInfo.branch_num + '.' + $scope.info.planInfo.plan_code;
                    $http.get(beetle_plan_url + '/project_plan/changePlanProjectToNewBranch?token_id=' + getTokenId() + '&plan_id=' + $scope.info.planId + '&plan_code=' + $scope.info.planInfo.plan_code + '&plan_name=' + $scope.info.planInfo.plan_name + '&trunk_name=' + $scope.info.trunkName + '&project_name=' + projectName + '&branchNum=' + $scope.info.proInfo.branch_num + '&fromProject=' + $scope.info.svn_form).success(function (data) {
                        if (data.success == true) {
                            $modalInstance.close();
                        } else {
                            $rootScope.showMessage("error", "失败", data.msg);
                        }
                    }).error(function (data) {
                        $rootScope.showMessage("error", "失败", data.message);
                    }).finally(function () {
                    })
                } else {
                    $http.get(beetle_plan_url + '/project_plan/changePlanProjectBranch?token_id=' + getTokenId() + '&trunk_name=' + $scope.info.trunkName + '&plan_id=' + $scope.info.planId + '&project_name=' + $scope.info.selectProName).success(function (data) {
                        if (!data.success) {
                            $rootScope.showMessage("error", "失败", data.msg);
                        }
                    }).error(function (data) {
                        $rootScope.showMessage("error", "失败", data.message);
                    }).finally(function () {
                        $modalInstance.close();
                    })
                }

            });
        });
    };

    $scope.checkProjectPlanCode = function () {
        if ($scope.info.planInfo.plan_code == '') {
            $rootScope.showMessage("error", "失败", "请输入项目代号！");
            return false;
        }
        var list = [$scope.info.trunkName];
        $scope.info.msg = '';
        var url = beetle_plan_url + "/project_plan/checkProjectPlanCode?token_id=" + getTokenId() + "&plan_code=" + $scope.info.planInfo.plan_code + "&project_names=" + JSON.stringify(list);
        $http.get(url).success(function (data) {
            if (!data.success) {
                $scope.info.msg = data.msg;
            }
        }).error(function (data) {
            $rootScope.showMessage("error", "失败", data.message);
        }).finally(function () {
            console.log("checkProjectPlanCode()结束！");
        })

    };

    $scope.saveClick = function () {
        $modalInstance.close();
    };

    $scope.cancelClick = function () {
        $modalInstance.dismiss("cancel");
    };
}]);
